package Test;
import javax.swing.*;
import java.awt.*;
import static java.lang.Math.*;
public class DemoTest extends JFrame {

            private static final long serialVersionUID = -1284128891908775645L;

            public static final int GAME_WIDTH = 500;
            public static final int GAME_HEIGHT = 500;
            public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
            public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;

            public DemoTest() {
                setTitle("心形曲线");
                setLocation((WIDTH - GAME_WIDTH) / 2, (HEIGHT - GAME_HEIGHT) / 2);
                setSize(GAME_WIDTH, GAME_HEIGHT);
                setBackground(Color.BLACK);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
            }

            @Override
            public void paint(Graphics g) {
                super.paint(g); // 调用超类的paint方法
                double x, y, r;
                Image OffScreen = createImage(GAME_WIDTH, GAME_HEIGHT);
                Graphics drawOffScreen = OffScreen.getGraphics();
                drawOffScreen.setColor(Color.PINK); // 将颜色设置移到循环外面
                for (int i = 0; i < 90; i++) {
                    for (int j = 0; j < 90; j++) {
                        r = PI / 45 * i * (1 - sin(PI / 45 * j)) * 18;
                        x = r * cos(PI / 45 * j) * sin(PI / 45 * i) + GAME_WIDTH / 2;
                        y = -r * sin(PI / 45 * j) + GAME_HEIGHT / 4;
                        drawOffScreen.fillOval((int) x, (int) y, 2, 2);
                    }
                }
                g.drawImage(OffScreen, 0, 0, this);
            }

            public static void main(String[] args) {
                SwingUtilities.invokeLater(() -> {
                    DemoTest demo = new DemoTest();
                });
            }
        }
